package com.rtp2p.rtcoresdkdemo.add.lan

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.util.Consumer
import androidx.lifecycle.LifecycleOwner
import com.rtp2p.rtcoresdkdemo.base.BaseRecyclerView
import com.rtp2p.rtcoresdkdemo.databinding.AddBindingApItemBinding

class AddBindingLanAdapter(val onDeviceId: Consumer<String>) :
    BaseRecyclerView.BaseAdapter<String, AddBindingLanAdapter.AddBindingVH>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddBindingVH {
        val binding = AddBindingApItemBinding.inflate(LayoutInflater.from(parent.context))
        binding.lifecycleOwner = parent.context as LifecycleOwner

        return AddBindingVH(binding)
    }

    override fun onBindViewHolder(holder: AddBindingVH, position: Int) {
        getData(position)?.let { holder.update(it) }
    }

    inner class AddBindingVH(binding: AddBindingApItemBinding) :
        BaseRecyclerView.BaseViewHolder<String, AddBindingApItemBinding>(binding) {
        override fun onChange(data: String) {
            binding.deviceId = data
            binding.root.setOnClickListener {
                onDeviceId.accept(data)
            }
        }
    }
}